package com.pds.web.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.utils.DateUtils;
import com.pds.common.utils.SecurityUtils;
import com.pds.web.monitor.domain.PdsMonitorTaskAnalysis;
import com.pds.web.monitor.mapper.PdsMonitorTaskAnalysisMapper;
import com.pds.web.monitor.service.IPdsMonitorTaskAnalysisService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class PdsMonitorTaskAnalysisServiceImpl extends ServiceImpl<PdsMonitorTaskAnalysisMapper, PdsMonitorTaskAnalysis> implements IPdsMonitorTaskAnalysisService {

    @Resource
    private PdsMonitorTaskAnalysisMapper pdsMonitorTaskAnalysisMapper;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public PdsMonitorTaskAnalysis addAnalysis(PdsMonitorTaskAnalysis pdsMonitorTaskAnalysis) {
//        Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
        Date createTime = DateUtils.parseDate(DateUtils.getTime());
        pdsMonitorTaskAnalysis.setCreateTime(createTime);
        pdsMonitorTaskAnalysisMapper.insert(pdsMonitorTaskAnalysis);
        return pdsMonitorTaskAnalysis;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public Boolean updateAnalysis(PdsMonitorTaskAnalysis pdsMonitorTaskAnalysis) {
        Date updateTime = DateUtils.parseDate(DateUtils.getTime());
        pdsMonitorTaskAnalysis.setUpdateTime(updateTime);
//        pdsMonitorTaskAnalysis.setStatus("2");
        return pdsMonitorTaskAnalysisMapper.updateById(pdsMonitorTaskAnalysis) > 0;
    }

//    @Override
//    public Boolean deleteAnalysis(Long id) {
//        return pdsMonitorTaskAnalysisMapper.deleteById(id) > 0;
//    }

    @Override
    public PdsMonitorTaskAnalysis listAnalysis(PdsMonitorTaskAnalysis pdsMonitorTaskAnalysis) {
        QueryWrapper<PdsMonitorTaskAnalysis> qw = new QueryWrapper<>();
        Long organizationId = null;
        Long taskId = pdsMonitorTaskAnalysis.getTaskId();
        if(!Objects.isNull(organizationId)){
            qw.eq("organization_id", organizationId);
        }
        if(!Objects.isNull(taskId)){
            qw.eq("task_id",taskId);
        }
        return pdsMonitorTaskAnalysisMapper.selectOne(qw);
    }
}
